import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../../../dd_core/dd_config.dart';
import '../../../dd_core/dd_hive_dao.dart';
import '../../../dd_models/dd_hive_works_model.dart';


class DDMyAticalController extends GetxController {

  final datas = <HiveWorksModel>[].obs;

  @override
  void onClose() {
    datas.close();
    super.onClose();
  }

  @override
  onInit() {
    super.onInit();
    getUserList();
  }

  getUserList() async {
    final prefs = await SharedPreferences.getInstance();
    String cureenUid = await prefs.getString(DDKey.cureenUid) ?? "";
    List<HiveWorksModel>? d = await DDHiveDao.getMyFencingList(
        uid: cureenUid,);
    if (d != null) {
      datas.value = d;
      datas.refresh();
    }
  }

  updateMyLike(HiveWorksModel m) async {
    await DDHiveDao.updateFencingLikes(hiveWorksModel: m);
    await getUserList();
  }

  removeMyArticles(HiveWorksModel m) async {
    EasyLoading.show();
    await Future.delayed(Duration(seconds: 1));
    EasyLoading.dismiss();
    await DDHiveDao.removeMyArticles(fencingid: m.fencingid!);
    await getUserList();
  }

}
